package f_collection;
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Vector;


public class HashMapExFrame extends JFrame {

	private JPanel contentPane;
	private JScrollPane scrollPane;
	private JScrollPane scrollPane_1;
	private JList member;
	private JButton addRoomBtn;
	private JButton delRoomBtn;
	private JButton addMemberBtn;
	private JButton delMemberBtn;
	private JList room;

	private HashMap<String, HashMap> hm_room;
	private HashMap<String, ObjectOutputStream> hm_member;
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					HashMapExFrame frame = new HashMapExFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public HashMapExFrame() {
		init();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getScrollPane());
		contentPane.add(getScrollPane_1());
		contentPane.add(getAddRoomBtn());
		contentPane.add(getDelRoomBtn());
		contentPane.add(getAddMemberBtn());
		contentPane.add(getDelMemberBtn());
	}
	
	public void init(){
		setTitle("HashMap & JList");
		
		// HashMap 초기값 생성
		hm_room = new HashMap<String, HashMap>();
		
		hm_member = new HashMap<String, ObjectOutputStream>();
		hm_member.put("김씨", null);
		hm_member.put("이씨", null);
		hm_member.put("최씨", null);
		hm_room.put("대기실", hm_member);

		
		hm_member = new HashMap<String, ObjectOutputStream>();
		hm_member.put("Kim", null);
		hm_member.put("Lee", null);
		hm_member.put("Choi", null);
		hm_member.put("Park", null);
		hm_room.put("초딩방", hm_member);

		
		hm_member = new HashMap<String, ObjectOutputStream>();
		hm_member.put("1111", null);
		hm_member.put("2222", null);
		hm_member.put("3333", null);
		hm_member.put("4444", null);
		hm_room.put("중딩방", hm_member);

		hm_member = new HashMap<String, ObjectOutputStream>();
		hm_member.put("aaaa", null);
		hm_member.put("bbbb", null);
		hm_member.put("cccc", null);
		hm_member.put("dddd", null);
		hm_room.put("고딩방", hm_member);
		
	}
	
	
	
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 10, 171, 165);
			scrollPane.setViewportView(getRoom());
		}
		return scrollPane;
	}
	private JScrollPane getScrollPane_1() {
		if (scrollPane_1 == null) {
			scrollPane_1 = new JScrollPane();
			scrollPane_1.setBounds(195, 10, 227, 165);
			scrollPane_1.setViewportView(getMember());
		}
		return scrollPane_1;
	}
	private JList getMember() {
		if (member == null) {
			member = new JList();
		}
		return member;
	}
	private JButton getAddRoomBtn() {
		if (addRoomBtn == null) {
			addRoomBtn = new JButton("\uBC29\uCD94\uAC00");
			addRoomBtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					// 방추가
					String str = 
							JOptionPane.showInputDialog("방이름을 입력하세요");
					hm_room.put(str, null);
					room.setListData(hm_room.keySet().toArray());
				}
			});
			addRoomBtn.setBounds(12, 183, 97, 23);
		}
		return addRoomBtn;
	}
	private JButton getDelRoomBtn() {
		if (delRoomBtn == null) {
			delRoomBtn = new JButton("\uBC29\uC0AD\uC81C");
			delRoomBtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					//방삭제
					String key = (String)room.getSelectedValue();	
					if(key == null){
						JOptionPane.showMessageDialog(HashMapExFrame.this, "삭제할 방을 선택하세요...");
					}else{
						hm_room.remove(key);
						room.setListData(hm_room.keySet().toArray());
					}
				
				
				}
			});
			delRoomBtn.setBounds(12, 216, 97, 23);
		}
		return delRoomBtn;
	}
	private JButton getAddMemberBtn() {
		if (addMemberBtn == null) {
			addMemberBtn = new JButton("\uC811\uC18D\uC790 \uCD94\uAC00");
			addMemberBtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
				// 접속자 추가
				// 선택된 방제목에 해당하는 HashMap 가져옴.
				String key = (String)room.getSelectedValue();	
				if( key != null){
					HashMap hm = hm_room.get(key);
					String str = JOptionPane.showInputDialog("접속자명");
					hm.put(str, null);
					member.setListData(hm.keySet().toArray());
				}
				
				
				}
			});
			addMemberBtn.setBounds(195, 183, 125, 23);
		}
		return addMemberBtn;
	}
	private JButton getDelMemberBtn() {
		if (delMemberBtn == null) {
			delMemberBtn = new JButton("\uC811\uC18D\uC790 \uC0AD\uC81C");
			delMemberBtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					// 접속자 제거
					String key = (String)member.getSelectedValue();
					if(key == null){
						JOptionPane.showMessageDialog(HashMapExFrame.this, "강퇴시킬 멤버가 선택되지 않았습니다.");
					}else{
						//룸에 해당하는 HashMap choice
						String roomKey = (String)room.getSelectedValue();
						HashMap hm = hm_room.get(roomKey);
						
						// 삭제전 대기실로 이동하기
						ObjectOutputStream moveObject = 
										(ObjectOutputStream)hm.get(key);
						HashMap guestRoom =
								hm_room.get("대기실");
						guestRoom.put(key, moveObject);
						
						// 현재 방에서 제거
						hm.remove(key);
						member.setListData(hm.keySet().toArray());
						
					}
				
				}
			});
			delMemberBtn.setBounds(195, 216, 125, 23);
		}
		return delMemberBtn;
	}
	private JList getRoom() {
		if (room == null) {
			room = new JList();
			
			room.setListData(hm_room.keySet().toArray());
			
			
			room.addMouseListener(new MouseAdapter() {
				@Override
				public void mousePressed(MouseEvent e) {
					String key = (String)room.getSelectedValue();
					
					HashMap hm = hm_room.get(key);
					member.setListData(new Vector());// 기존항목 무조건 삭제
					if(hm != null){ // 하위 항목이 있으면
						member.setListData(hm.keySet().toArray());
					}
				}
			});
		}
		return room;
	}
}



